import { Menu, Badge } from 'antd';
import React from 'react';
import { useModel, history } from 'umi';
import _ from 'lodash';
export type SiderTheme = 'light' | 'dark';

const TodoList: React.FC = () => {
  const { initialState } = useModel('@@initialState');
  const { flowDoing } = useModel('sysInfo', (ret) => ({
    flowDoing: ret.flowDoing,
  }));
  const sysInfo = initialState?.sys;
  if (_.isEmpty(sysInfo)) {
    return null;
  }
  const menuDatas = sysInfo?.menuDatas;
  if (_.isEmpty(menuDatas)) {
    return null;
  }
  const handleMenuClick = ({ key }: any) => {
    const ids = key.split('_');
    if (!ids && ids.length !== 3) {
      return;
    }
    let permid = Number(ids[1]);
    const stepID = Number(ids[2]);
    if (stepID === 1) {
      permid = permid - 1;
    }
    const findMenu = (mDatas: API.MenuData[]): API.MenuData | undefined => {
      for (let index = 0; index < mDatas.length; index++) {
        if (mDatas[index].routes) {
          const mData = findMenu(mDatas[index].routes!);
          if (mData) {
            return mData;
          }
        } else {
          if (mDatas[index].id === permid) {
            return mDatas[index];
          }
        }
      }
      return undefined;
    };
    const mData = findMenu(menuDatas!);
    const path = mData?.path;
    history.push(path!);
  };
  const mItems: JSX.Element[] = [];
  if (!_.isEmpty(flowDoing?.list)) {
    flowDoing?.list.forEach((d) => {
      mItems.push(
        <Menu.Item key={'' + d.WFID + '_' + d.PermID + '_' + d.STEPID}>
          {`${d.WFNM}`}
          <Badge count={d.NUM} />
        </Menu.Item>,
      );
    });
  }
  return (
    <Menu onClick={handleMenuClick}>
      {mItems}
      <Menu.Item key="-6">更多...</Menu.Item>
    </Menu>
  );
};
export default TodoList;
